package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.trainorganinfo.MultiBoundTrainOrganInfoException;
import com.xinzhitong.www.pojo.TrainOrgan;
import com.xinzhitong.www.service.TrainOrganUserInfoService;
import com.xinzhitong.www.utils.Message;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 机构用户信息控制器
 *
 * @author 徐鑫，黄欢欢,吴展鹏
 */
@CrossOrigin
@RestController
@RequestMapping("organUserInfo")
public class OrganUserInfoController {

    @Resource
    TrainOrganUserInfoService trainOrganUserInfoService;

    /**
     * 完善机构信息
     *
     * @param logo         logo
     * @param name         全称
     * @param shortname    简称
     * @param date         成立时间
     * @param province     省
     * @param city         市
     * @param area         区
     * @param address      详细地址
     * @param contact      联系方式
     * @param introduction 简介
     * @param pictures     图片列表
     * @return 响应信息
     */
    @RequiresRoles(value = {"ORGAN"})
    @ResponseBody
    @RequestMapping(value = "createTrainOrganInfo", method = RequestMethod.PUT)
    public Message createTrainOrganInfo(
            @RequestParam("logo") MultipartFile logo,
            @RequestParam("name") String name,
            @RequestParam("shortname") String shortname,
            @RequestParam("date") String date,
            @RequestParam("province") String province,
            @RequestParam("city") String city,
            @RequestParam("area") String area,
            @RequestParam("address") String address,
            @RequestParam("contact") String contact,
            @RequestParam("introduction") String introduction,
            @RequestParam("pictures") MultipartFile[] pictures
    ) {
        String msg = "修改信息成功";
        TrainOrgan trainOrgan = new TrainOrgan(
                name,
                shortname,
                date,
                province,
                city,
                area,
                address,
                contact,
                introduction
        );
        try {
            trainOrganUserInfoService.createTrainOrganUserInfo(trainOrgan, logo, pictures);
            return Message.createSuc(msg);
        } catch (MultiBoundTrainOrganInfoException e) {
            e.printStackTrace();
            msg = "您已创建过培训机构信息，请勿重复创建";
        }
        return Message.createErr(msg);
    }

    /**
     * 完善机构信息  存在bug
     *
     * @param logoNum      logo
     * @param name         全称
     * @param shortname    简称
     * @param date         成立时间
     * @param province     省
     * @param city         市
     * @param area         区
     * @param address      详细地址
     * @param contact      联系方式
     * @param introduction 简介
     * @param picturesNum     图片列表
     * @return 响应信息
     */
    @RequiresRoles(value = {"ORGAN"})
    @ResponseBody
    @RequestMapping(value = "createTrainOrganInfoAccelerate", method = RequestMethod.PUT)
    public Message createTrainOrganInfoAcceleratePictures(
            @RequestParam("logoNum") Integer logoNum,
            @RequestParam("name") String name,
            @RequestParam("shortname") String shortname,
            @RequestParam("date") String date,
            @RequestParam("province") String province,
            @RequestParam("city") String city,
            @RequestParam("area") String area,
            @RequestParam("address") String address,
            @RequestParam("contact") String contact,
            @RequestParam("introduction") String introduction,
            @RequestParam("picturesNum") Integer picturesNum
    ) {
        String msg = "修改信息成功";
        TrainOrgan trainOrgan = new TrainOrgan(
                name,
                shortname,
                date,
                province,
                city,
                area,
                address,
                contact,
                introduction
        );
        try {
            trainOrganUserInfoService.createTrainOrganUserInfoAcceleratePictures(trainOrgan, logoNum, picturesNum);
            return Message.createSuc(msg);
        } catch (MultiBoundTrainOrganInfoException e) {
            e.printStackTrace();
            msg = "您已创建过培训机构信息，请勿重复创建";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取账号概要
     *
     * @return 账号概要两个主要信息
     */
    @RequiresRoles(value = {"ORGAN"})
    @RequestMapping(value = "trainOrganAccountView", method = RequestMethod.GET)
    public Message trainOrganAccountView() {
        return Message.createSuc(trainOrganUserInfoService.getView());
    }

    /**
     * 获取账号信息
     *
     * @return 账号信息
     */
    @RequiresRoles(value = {"ORGAN"})
    @RequestMapping(value = "trainOrganUserInfo", method = RequestMethod.GET)
    public Message trainOrganUserInfo() {
        return Message.createSuc(trainOrganUserInfoService.getInfo());
    }

    /**
     * 获取机构名称和机构id
     *
     * @return name，id
     */
    @RequestMapping(value="trainOrganName",method =RequestMethod.GET)
    public Message trainOrganNameAndId(){
        return Message.createSuc(trainOrganUserInfoService.getNameAndId());
    }

}
